package jdbc;

import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Hello {
    public static void main(String[] args) throws SQLException {
//        testInsert();
//        testDelete();
//        testUpdate();
        testSelect();
    }

    private static void testInsert() throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("666666");

        Connection connection = dataSource.getConnection();

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学号！");
        int id = scanner.nextInt();
        System.out.println("请输入姓名！");
        String name = scanner.next();

        String sql = "insert into student values (?,?)";

        PreparedStatement statement = connection.prepareStatement(sql);

        statement.setInt(1, id);
        statement.setString(2, name);
        System.out.println("sql:" + statement);

        int ret = statement.executeUpdate();
        System.out.println("ret= " + ret);

        statement.close();
        connection.close();
    }

    public static void testDelete() throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("666666");

        Connection connection = dataSource.getConnection();

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的id ");
        scanner.nextInt();

        String sql = "delete from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, 56);

        int ret = statement.executeUpdate();
        System.out.println("ret=" + ret);

        connection.close();
        statement.close();
    }

    public static void testUpdate() throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("666666");

        Connection connection = dataSource.getConnection();

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的id");
        int id = scanner.nextInt();
        System.out.println("请输入要修改的姓名");
        String name = scanner.next();

        String sql = "update student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, name);
        statement.setInt(2, id);

        int ret = statement.executeUpdate();
        System.out.println("ret=" + ret);
        System.out.println(statement);
        connection.close();
        statement.close();
    }

    public static void testSelect() throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("666666");

        Connection connection = dataSource.getConnection();

        String sql = "select * from student";

        PreparedStatement statement = connection.prepareStatement(sql);

        ResultSet resultSet = statement.executeQuery();

        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id: " + id + " name:" + name);
        }
        connection.close();
        statement.close();
    }
}
